﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EffiProz.Core.Lib.IO
{
    public class DataInputStream : Stream
    {
        byte[] _tempBuffer = new byte[8];
        Stream _stream;

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _stream.Seek(offset, origin);
        }


        public override void Flush()
        {
            _stream.Flush();
        }


        public override long Position
        {
            get
            {
                return _stream.Position;
            }

            set
            {
                _stream.Position = value;
            }
        }

        public override long Length
        {
            get
            {
                return _stream.Length;
            }
        }

        public override bool CanRead
        {
            get
            {
                return _stream.CanRead;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return _stream.CanSeek;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return false;
            }
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long value)
        {
            _stream.SetLength(value);
        }

        public int ReadFully(byte[] buffer, int offset, int count)
        {
            int len =0;
            int cnt = count;
            do
            {
                len = _stream.Read(buffer, offset, cnt);
                cnt -= len;
                offset += len;
            }
            while (len > 0);

            return count - cnt;             
        }


        public override int Read(byte[] buffer, int offset, int count)
        {
            return _stream.Read(buffer, offset, count);
        }

        public DataInputStream(Stream stream)
        {
            _stream = stream;
        }

        public override int ReadByte()
        {
            return (int)_stream.ReadByte();
        }

        public int ReadInt()
        {    
            _stream.Read(_tempBuffer,0,4);
            return BitConverter.ToInt32(_tempBuffer,0);           
        }

        public long ReadLong()
        {
           _stream.Read(_tempBuffer,0,8);
            return BitConverter.ToInt64(_tempBuffer,0); 
        }

        public Char ReadChar()
        {
            _stream.Read(_tempBuffer,0,2);
            return BitConverter.ToChar(_tempBuffer,0); 
        }              

         public double ReadDouble()
        {
            _stream.Read(_tempBuffer,0,8);
            return BitConverter.ToDouble(_tempBuffer,0); 
        }              

        public void Read(byte[] bytes)
        {
            _stream.Read(bytes, 0, bytes.Length);
        }      
         
    }
}
